/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;
import java.awt.Color;
import javax.swing.*;
import java.io.*;
import javax.swing.table.*;

/**
 *
 * @author Sean
 */
public class UseCaseController {

    private static UseCase myUC;

    public static void UseCaseController(){
        return;
    }

    public static void Open(JTextPane reqTextPane){
        reqTextPane.setEnabled(true);
        JFrame frame = new JFrame();
        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showOpenDialog(frame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File newFile = chooser.getSelectedFile();
            try{
                // Open the file that is the first
                // command line parameter
                FileInputStream fstream = new FileInputStream(newFile);
                // Get the object of DataInputStream
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                //Read File Line By Line
                while ((strLine = br.readLine()) != null)   {
                  // Print the content on the console
                  //System.out.println (strLine);
                  reqTextPane.replaceSelection(strLine);
                  reqTextPane.replaceSelection("\n");
                }
                //Close the input stream
                in.close();
              }
            catch (Exception e){//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }
        }
    }

    public static void Enable(JTextPane reqTextPane){
        reqTextPane.setEnabled(true);
    }

    public static void startUseCase(JButton actorButton, JButton verbButton, JButton systemButton, JButton deriveButton, DefaultTableModel model, JTable useCaseTable,JButton nounButton){
        myUC = new UseCase();
        UseCaseGUI.incUseCaseCount();
        model.addRow(new Object[]{"","","",false});
        actorButton.setEnabled(true);  //enable actor button
        verbButton.setEnabled(true);  //enable verb noun phrase button
        systemButton.setEnabled(true);  //enable system button
        nounButton.setEnabled(true);
        deriveButton.setEnabled(false);

    }

    public static void actorPressed(JTextPane reqTextPane, JButton actorButton, JButton verbButton, JButton systemButton, JButton deriveButton, JTable useCaseTable, UseCaseGUI frame, JButton nounButton){
        String actor = reqTextPane.getSelectedText();
        int curLocation = reqTextPane.getSelectionStart();
        if(!systemButton.isEnabled() && !verbButton.isEnabled() && !nounButton.isEnabled())
        {
            String message = "Abstraction of a business process?\n" +
                             "Initiated by an actor?\n" +
                             "Accomplishes some business task for the actor?\n" +
                             "Ends with the actor?";

            int answer = JOptionPane.showConfirmDialog(frame, message);
            if (answer == JOptionPane.YES_OPTION) {
                useCaseTable.setValueAt(true, UseCaseGUI.getUseCaseCount(), 3);
                deriveButton.setEnabled(true);                
            } else if (answer == JOptionPane.NO_OPTION) {
                deriveButton.setEnabled(true);                
            }
            else if (answer == JOptionPane.CANCEL_OPTION) {
                return;
            }
        }        
        actorButton.setEnabled(false);
        //reqTextPane.replaceSelection(actor);
        
        if (!reqTextPane.getSelectedText().equals("")) {
            ((gui.ColorPane) reqTextPane).changeSelectedColor(Color.green);
        }
        useCaseTable.setValueAt(actor, UseCaseGUI.getUseCaseCount(), 0);
        reqTextPane.select(-1, -1);
        reqTextPane.setCaretPosition(curLocation);
    }

    public static void verbPressed(JTextPane reqTextPane, JButton actorButton, JButton verbButton, JButton systemButton, JButton deriveButton, JTable useCaseTable, UseCaseGUI frame, JButton nounButton){
        String verb = reqTextPane.getSelectedText();
        int curLocation = reqTextPane.getSelectionStart();
        if(!actorButton.isEnabled() && !systemButton.isEnabled() && !nounButton.isEnabled())
        {
            String message = "Abstraction of a business process?\n" +
                             "Initiated by an actor?\n" +
                             "Accomplishes some business task for the actor?\n" +
                             "Ends with the actor?";

            int answer = JOptionPane.showConfirmDialog(frame, message);
            if (answer == JOptionPane.YES_OPTION) {
                useCaseTable.setValueAt(true, UseCaseGUI.getUseCaseCount(), 3);
                deriveButton.setEnabled(true);               
            } else if (answer == JOptionPane.NO_OPTION) {
                deriveButton.setEnabled(true);               
            }
            else if (answer == JOptionPane.CANCEL_OPTION) {
                return;
            }
        }
        verbButton.setEnabled(false);
        //TextPane1.replaceSelection(actor);
        
        if (!reqTextPane.getSelectedText().equals("")) {
            ((gui.ColorPane) reqTextPane).changeSelectedColor(Color.red);
        }
        if ((useCaseTable.getValueAt(UseCaseGUI.getUseCaseCount(), 1).toString()) != ""){
            verb = verb.concat(useCaseTable.getValueAt(UseCaseGUI.getUseCaseCount(), 1).toString());
        }
        useCaseTable.setValueAt(verb, UseCaseGUI.getUseCaseCount(), 1);
        reqTextPane.select(-1, -1);
        reqTextPane.setCaretPosition(curLocation);
    }

    public static void nounPressed(JTextPane reqTextPane, JButton actorButton, JButton verbButton, JButton systemButton, JButton deriveButton, JTable useCaseTable, UseCaseGUI frame, JButton nounButton){
        String noun = reqTextPane.getSelectedText();
        int curLocation = reqTextPane.getSelectionStart();
        if(!actorButton.isEnabled() && !systemButton.isEnabled() && !verbButton.isEnabled())
        {
            String message = "Abstraction of a business process?\n" +
                             "Initiated by an actor?\n" +
                             "Accomplishes some business task for the actor?\n" +
                             "Ends with the actor?";

            int answer = JOptionPane.showConfirmDialog(frame, message);
            if (answer == JOptionPane.YES_OPTION) {
                useCaseTable.setValueAt(true, UseCaseGUI.getUseCaseCount(), 3);
                deriveButton.setEnabled(true);
            } else if (answer == JOptionPane.NO_OPTION) {
                deriveButton.setEnabled(true);
            }
            else if (answer == JOptionPane.CANCEL_OPTION) {
                return;
            }
        }
        nounButton.setEnabled(false);
        //TextPane1.replaceSelection(actor);

        if (!reqTextPane.getSelectedText().equals("")) {
            ((gui.ColorPane) reqTextPane).changeSelectedColor(Color.red);
        }

        noun = useCaseTable.getValueAt(UseCaseGUI.getUseCaseCount(), 1).toString().concat(" "+noun);
        useCaseTable.setValueAt(noun, UseCaseGUI.getUseCaseCount(), 1);
        reqTextPane.select(-1, -1);
        reqTextPane.setCaretPosition(curLocation);
    }

    public static void systemPressed(JTextPane reqTextPane, JButton actorButton, JButton verbButton, JButton systemButton, JButton deriveButton, JTable useCaseTable, UseCaseGUI frame, JButton nounButton){
        String system = reqTextPane.getSelectedText();
        int curLocation = reqTextPane.getSelectionStart();
        if(!actorButton.isEnabled() && !verbButton.isEnabled() && !nounButton.isEnabled())
        {
            String message = "Abstraction of a business process?\n" +
                             "Initiated by an actor?\n" +
                             "Accomplishes some business task for the actor?\n" +
                             "Ends with the actor?";

            int answer = JOptionPane.showConfirmDialog(frame, message);
            if (answer == JOptionPane.YES_OPTION) {
                useCaseTable.setValueAt(true, UseCaseGUI.getUseCaseCount(), 3);
                deriveButton.setEnabled(true);                
            } else if (answer == JOptionPane.NO_OPTION) {
                deriveButton.setEnabled(true);                
            }
            else if (answer == JOptionPane.CANCEL_OPTION) {
                return;
            }            
        }
        
        systemButton.setEnabled(false);
        //reqTextPane.replaceSelection(actor);
        
        if (!reqTextPane.getSelectedText().equals("")) {
            ((gui.ColorPane) reqTextPane).changeSelectedColor(Color.blue);
        }
        useCaseTable.setValueAt(system, UseCaseGUI.getUseCaseCount(), 2);
        reqTextPane.select(-1, -1);
        reqTextPane.setCaretPosition(curLocation);
    }

    public static void generatePressed(JTabbedPane jTabbedPane1){
       DrawPanel diagPanel = new DrawPanel(jTabbedPane1);
       JLabel diagLabel = new JLabel("Diagram goes here");
       diagPanel.add(diagLabel);
       jTabbedPane1.add("Diagram", diagPanel);
       jTabbedPane1.setSelectedIndex(1);
    }

    public void closePressed(JTabbedPane jTabbedPane1){
       jTabbedPane1.remove(1);       
    }

}
